/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.webchat.exception;

import io.webchat.wdzl.utils. IWdzlJSONResult ;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * 异常处理器
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestControllerAdvice
@Slf4j
public class RRExceptionHandler {
	private Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 处理自定义异常
	 */
	@ExceptionHandler(RRException.class)
	public  IWdzlJSONResult  handleRRException(RRException e){
		 IWdzlJSONResult  iWdzlJSONResult  = new  IWdzlJSONResult();
		 iWdzlJSONResult.put("code", e.getCode());
		 iWdzlJSONResult.put("msg", e.getMessage());

		return  iWdzlJSONResult ;
	}

	@ExceptionHandler(NoHandlerFoundException.class)
	public IWdzlJSONResult handlerNoFoundException(Exception e) {
		logger.error(e.getMessage(), e);
		return IWdzlJSONResult.error(404, "路径不存在，请检查路径是否正确");
	}

	@ExceptionHandler(DuplicateKeyException.class)
	public IWdzlJSONResult handleDuplicateKeyException(DuplicateKeyException e){
		logger.error(e.getMessage(), e);
		return IWdzlJSONResult.error("数据库中已存在该记录");
	}


	@ExceptionHandler(Exception.class)
	public IWdzlJSONResult handleException(Exception e){
		logger.error(e.getMessage(), e);
		return IWdzlJSONResult.error();
	}
}
